package member;

import java.util.ArrayList;

import org.apache.ibatis.session.SqlSession;

// 회정정보 입력, 수정 등을 처리하는 모든 제어구를 가진 곳
public class MemberMyba {

	private SqlSession session;  // 커넥션 객체가 생성된 상태
	MemberVo memberVo;
	

	public MemberMyba(){
		try{
			// MemberFactory 에서 factory의 session을 오픈
			session = MemberFactory.getFactory().openSession();
		}catch(Exception ex){
			
		}
	}
	
	public MemberVo view(){
		MemberVo vo = null;
		
		try{
			// memberVo의 memberid값을 가지고 오라는 명령
			vo = (MemberVo)session.selectOne("member.view", memberVo.getMemberid());
			session.close();
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		return vo;
	}
	
	
	public ArrayList<MemberVo> list(){
		ArrayList<MemberVo> data = null;
		try{
			// session 은 생성자가 돌때 생성, member 는 mapper의 namespce 값이고, list 는 id 값이다.
			data = (ArrayList<MemberVo>)session.selectList("member.list","%" + memberVo.getFind() + "%");
			
			session.close();
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		
		return data;
	}
	
	//  ------------------------------------ 수정 ------------------------------------ 
	public int modify(){
		int cnt = 0;
		try{
			cnt = session.update("member.modify", memberVo);
			
			
			if(cnt>0) session.commit();
			else session.rollback();
			
			session.close();
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		return cnt;
	}
	
	//  ------------------------------------ 삭제 ------------------------------------ 
	public int del(){
		int cnt = 0;
		
		try{
			cnt = session.delete("member.delete", memberVo.getMemberid());
			if(cnt>0) session.commit();
			else session.rollback();
			
			session.close();
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		return cnt;
	}
	
	// ------------------------------------ 삽입 ------------------------------------ 
	public int insert(){
		int cnt = 0;
		
		try{
			cnt = session.insert("member.insert", memberVo);
			if(cnt>0) session.commit();
			else session.rollback();
			
			session.close();
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		return cnt;
	}
	
	public MemberVo getMemberVo() {
		return memberVo;
	}

	public void setMemberVo(MemberVo memberVo) {
		this.memberVo = memberVo;
	}

}
